package com.utils;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;

public class ResultJson {
	/** 响应状态 ，100成功，200失败.默认是成功状态。json必定有参数 */
	private int status = 100;
	/**
	 * 统计数量
	 */
	private int count = -1;

	/** 当前页码 */
	private int pageNo = 1;

	/** 具体数据。json必定有的参数 */
	private Object rows;

	private Object toList;

	/** 分页的页数，默认为-1，不分页。json可能有的参数 */
	private int total = -1;

	/** 响应消息。json可能有的参数 */
	private String message = null;

	Map<String, Object> jsonMap = new HashMap<String, Object>();

	public ResultJson() {
	}

	/**
	 * 只有数据的情况下默认是成功
	 * 
	 * @param rows
	 *            数据
	 */
	public ResultJson(Object rows) {
		this.status = 100;
		this.rows = rows;
	}

	/**
	 * @param status
	 *            状态
	 * @param rows
	 *            数据
	 */
	public ResultJson(int status, Object rows) {
		this.status = status;
		this.rows = rows;
	}

	/**
	 * @param status
	 *            状态
	 * @param rows
	 *            数据
	 * @param toList
	 *            已在列表数据
	 */
	public ResultJson(int status, Object rows, Object toList) {
		this.status = status;
		this.rows = rows;
		this.toList = toList;
	}

	/**
	 * @param status
	 *            状态
	 * @param rows
	 *            数据
	 * @param message
	 *            提示消息
	 */
	public ResultJson(int status, Object rows, String message) {
		this.status = status;
		this.rows = rows;
		this.message = message;
	}

	/**
	 * @param status
	 *            状态
	 * @param message
	 *            提示消息
	 */
	public ResultJson(int status, String message) {
		this.status = status;
		this.message = message;
	}

	/**
	 * @param status
	 *            状态
	 * @param total
	 *            分页总数
	 * @param rows
	 *            数据
	 * @param message
	 *            提示消息
	 */
	public ResultJson(int status, int total, Object rows, String message) {
		this.status = status;
		this.total = total;
		this.rows = rows;
		this.message = message;
	}

	/**
	 * @param status
	 *            状态
	 * @param total
	 *            分页总数
	 * @param rows
	 *            数据
	 */
	public ResultJson(int status, int pageNo, int total, Object rows) {
		this.status = status;
		this.total = total;
		this.rows = rows;
		this.pageNo = pageNo;
	}

	/**
	 * 
	 * @param status
	 *            状态
	 * @param count
	 *            数量
	 */
	/*
	 * public ResultJson(int status, int count) {
	 * 
	 * this.status=status; this.count=count; }
	 */

	/**
	 * 
	 * @param status
	 *            状态
	 * @param count
	 *            数量
	 * @param rows
	 *            数据
	 */
	public ResultJson(int status, int count, Object rows) {

		this.status = status;
		this.rows = rows;
		this.count = count;
	}

	/**
	 * @描述:普通的json数据返回
	 * @创建时间：2018-9-20上午7:37:58
	 * @参数： @return
	 * @参数： @throws Exception
	 */
	public String getCommonJson() throws Exception {
		jsonMap.put("status", status);
		if (StringUtils.isNotBlank(message)) {
			jsonMap.put("message", message);
		}

		if (total > -1) {
			jsonMap.put("pager.pageNo", pageNo);
			jsonMap.put("pager.totalRows", total);
		}
		if (count > -1)
			jsonMap.put("count", count);

		jsonMap.put("rows", rows == null ? "" : rows);

		return ResultJson.returnJson(jsonMap);
	}

	public String getCommonJson(String myrows) throws Exception {
		jsonMap.put("status", status);
		if (StringUtils.isNotBlank(message)) {
			jsonMap.put("message", message);
		}

		if (total > -1) {
			jsonMap.put("pager.pageNo", pageNo);
			jsonMap.put("pager.totalRows", total);
		}

		jsonMap.put(myrows, rows == null ? "" : rows);

		return ResultJson.returnJson(jsonMap);
	}

	public String getCommonJson(String myrows, String mytoList)
			throws Exception {
		jsonMap.put("status", status);
		if (StringUtils.isNotBlank(message)) {
			jsonMap.put("message", message);
		}

		if (total > -1) {
			jsonMap.put("pager.pageNo", pageNo);
			jsonMap.put("pager.totalRows", total);
		}
		jsonMap.put(mytoList, toList == null ? "" : toList);
		jsonMap.put(myrows, rows == null ? "" : rows);

		return ResultJson.returnJson(jsonMap);
	}

	/**
	 * @描述:针对校验的Json返回
	 * @创建时间：2018-9-20上午7:37:45
	 * @参数： @param flag
	 * @参数： @return
	 * @参数： @throws Exception
	 */
	public String validJson(Boolean flag) throws Exception {
		Map<String, Object> jsonMap2 = new HashMap<String, Object>();
		jsonMap2.put("valid", flag);
		jsonMap.put("validateResult", jsonMap2);
		return ResultJson.returnJson(jsonMap);
	}

	/**
	 * @描述:下拉框的Json数据
	 * @创建时间：2018-9-20上午7:43:52
	 * @参数： @param jsonMap2
	 * @参数： @return
	 * @参数： @throws Exception
	 */
	public String dropDown(Object object) throws Exception {
		jsonMap.put("list", object);
		return ResultJson.returnJson(jsonMap);
	}

	public static String returnJson(Object object) throws Exception {
		ObjectMapper objectMapper = new ObjectMapper();
		StringWriter stringWriter = new StringWriter();
		objectMapper.writeValue(stringWriter, object);
		return stringWriter.toString();
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public Object getRows() {
		return rows;
	}

	public void setRows(Object rows) {
		this.rows = rows;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

}